package com.etime.shycourse.services.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.dao.LiveRecordMapper;
import com.etime.shycourse.dto.LiveRecordDto;
import com.etime.shycourse.pojo.LiveRecord;
import com.etime.shycourse.services.interfaces.LiveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 直播课程参与记录服务层实现类
 */
@Service
public class LiveRecordServiceImpl implements LiveRecordService {

    @Autowired
    private LiveRecordMapper liveRecordMapper;

    /**
     * 添加直播课程参与记录
     *
     * @param liveRecord
     * @return
     */
    @Override
    public Integer insertLiveRecord(LiveRecord liveRecord) {
        return liveRecordMapper.insert(liveRecord);
    }

    /**
     * 查询直播课程参与记录。
     *
     * @param liveRecord 直播题目，直播间id，直播/回放；进入/退出
     * @return
     */
    @Override
    public List<LiveRecord> selectLiveRecodeList(LiveRecord liveRecord) {
        return null;
    }

    /**
     * 直播记录日志 分页
     * <p></p>
     * 查询的条件:可根据
     * 直播间id liverecordid
     * 直播间名模糊搜索 livetitle
     * 直播间老师名模糊搜索 livename
     * 直播间参与学生名名模糊搜索 liveusername
     * 回放&直播 liveorreview
     * 进入&退出 liveinorout
     * <p>
     * wb
     *
     * @param pageNum,
     * @param pageSize      一
     * @param liveRecordDto
     * @return Page<LiveRecordDto>
     */
    @Override
    public Page<LiveRecordDto> selectLiveRecordDtoPage(LiveRecordDto liveRecordDto, Long pageNum, Long pageSize) {
        Page<LiveRecordDto> page;
        if (pageNum != null && pageSize != null) {
            page = new Page<>(pageNum, pageSize);
        } else {
            page = new Page<>();
        }
        if (liveRecordDto.getLivetitle() != null) {
            liveRecordDto.setLivetitle("%" + liveRecordDto.getLivetitle().trim() + "%");
        }
        if (liveRecordDto.getLiveusername() != null) {
            liveRecordDto.setLiveusername("%" + liveRecordDto.getLiveusername().trim() + "%");
        }
        if (liveRecordDto.getLivename() != null) {
            liveRecordDto.setLivename(liveRecordDto.getLivename().trim());
        }
        // 进行数据过滤 设置进入退出为空
        if (liveRecordDto.getLiveinorout() == -1) {
            liveRecordDto.setLiveinorout(null);
        }
        //拿到分页的数据
        Page<LiveRecordDto> dtoPage = liveRecordMapper.selectLiveRecordDtoPage(liveRecordDto, page);
        List<LiveRecordDto> list = dtoPage.getRecords();
        if (list.size() > 0) {
            //  对数据进行处理 处理名称
            list.forEach(l -> {
                if (l.getUsertruename() != null || l.getNickname() != null) {
                    if (l.getNickname() != null) {
                        if (l.getUsertruename() == null) {
                            l.setUsertruename(l.getNickname());
                        } else {
                            l.setUsertruename(l.getUsertruename() + "(" + l.getNickname() + ")");
                        }
                    }
                }
            });
        }
        return dtoPage;
    }
}
